/**
 *
 *
 * 
 *
 * 
 */

package io.dbw.common.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.poi.ss.formula.functions.T;

import java.io.Serializable;
import java.util.List;

/**
 * 分页工具类
 *
 * 
 */
@Data
public class PageUtils<T>  {
	@ApiModelProperty(value = "总记录数")
	private int totalCount;
	@ApiModelProperty(value = "每页记录数")
	private int pageSize;
	@ApiModelProperty(value = "总页数")
	private int totalPage;
	@ApiModelProperty(value = "当前页")
	private int currPage;
	@ApiModelProperty(value = "返回对象集合")
	private List<T> list;



	/**
	 * 分页
	 * @param list        列表数据
	 * @param totalCount  总记录数
	 * @param pageSize    每页记录数
	 * @param currPage    当前页数
	 */
	public PageUtils(List<T> list, int totalCount, int pageSize, int currPage) {
		this.list = list;
		this.totalCount = totalCount;
		this.pageSize = pageSize;
		this.currPage = currPage;
		this.totalPage = (int)Math.ceil((double)totalCount/pageSize);
	}

	/**
	 * 分页
	 */
	public PageUtils(IPage<T> page) {
		this.list = page.getRecords();
		this.totalCount = (int)page.getTotal();
		this.pageSize = (int)page.getSize();
		this.currPage = (int)page.getCurrent();
		this.totalPage = (int)page.getPages();
	}


	/**
	 * 开始分页
	 *
	 * @param list
	 * @param pageNo  页码
	 * @param pageSize 每页多少条数据
	 * @return
	 */
	public PageUtils(List<T> list, Integer pageNo,
					Integer pageSize) {
		// 记录总数
		Integer count = list.size();
		// 页数
		Integer pageCount = 0;
		if (count % pageSize == 0) {
			pageCount = count / pageSize;
		} else {
			pageCount = count / pageSize + 1;
		}
		// 开始索引
		int fromIndex = 0;
		// 结束索引
		int toIndex = 0;

		if (!pageNo.equals(pageCount)) {
			fromIndex = (pageNo - 1) * pageSize;
			toIndex = fromIndex + pageSize;
		} else {
			fromIndex = (pageNo - 1) * pageSize;
			toIndex = count;
		}
		List<T> pageList = list.subList(fromIndex, toIndex);
		this.list = pageList;
		this.totalCount = count;
		this.pageSize = pageSize;
		this.currPage = pageNo;
		this.totalPage = (int)Math.ceil((double)totalCount/pageSize);
	}
	
}
